from django.contrib import admin
from .models import Book, BookImage, BookNote, BookHistory


class BookImageInline(admin.TabularInline):
    """书籍图片内联"""
    model = BookImage
    extra = 1
    fields = ('image', 'image_type', 'uploaded_at')
    readonly_fields = ('uploaded_at',)


class BookNoteInline(admin.TabularInline):
    """书籍笔记内联"""
    model = BookNote
    extra = 0
    fields = ('chapter', 'note_type', 'note_image', 'created_at')
    readonly_fields = ('created_at',)


@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    """书籍管理"""
    
    list_display = (
        'id', 'title', 'author', 'seller', 'price', 
        'condition', 'status', 'view_count', 'favorite_count', 'created_at'
    )
    list_filter = ('status', 'condition', 'category', 'major', 'created_at')
    search_fields = ('title', 'author', 'publisher', 'isbn', 'seller__username')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('isbn', 'title', 'author', 'publisher', 'publish_year', 'edition')
        }),
        ('分类信息', {
            'fields': ('category', 'major', 'description')
        }),
        ('交易信息', {
            'fields': ('seller', 'original_price', 'price', 'condition', 'condition_score')
        }),
        ('状态信息', {
            'fields': ('status', 'view_count', 'favorite_count', 'cover_image')
        }),
        ('AI相关', {
            'fields': ('ai_price_suggestion', 'defect_report', 'sale_probability'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at', 'sold_at')
        }),
    )
    
    readonly_fields = ('created_at', 'updated_at', 'view_count', 'favorite_count')
    inlines = [BookImageInline, BookNoteInline]
    list_per_page = 20
    date_hierarchy = 'created_at'


@admin.register(BookImage)
class BookImageAdmin(admin.ModelAdmin):
    """书籍图片管理"""
    
    list_display = ('id', 'book', 'image_type', 'uploaded_at')
    list_filter = ('image_type', 'uploaded_at')
    search_fields = ('book__title',)
    ordering = ('-uploaded_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('book', 'image', 'image_type')
        }),
        ('AI检测', {
            'fields': ('defect_annotations',),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ('uploaded_at',)
    list_per_page = 30


@admin.register(BookNote)
class BookNoteAdmin(admin.ModelAdmin):
    """书籍笔记管理"""
    
    list_display = ('id', 'book', 'chapter', 'note_type', 'created_at')
    list_filter = ('note_type', 'created_at')
    search_fields = ('book__title', 'chapter', 'extracted_text')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('基本信息', {
            'fields': ('book', 'chapter', 'page_range', 'note_type')
        }),
        ('笔记内容', {
            'fields': ('note_image', 'extracted_text', 'keywords')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20


@admin.register(BookHistory)
class BookHistoryAdmin(admin.ModelAdmin):
    """书籍流转历史管理"""
    
    list_display = (
        'id', 'book', 'previous_owner', 'transaction_price', 
        'condition_at_sale', 'transaction_date'
    )
    list_filter = ('condition_at_sale', 'transaction_date')
    search_fields = ('book__title', 'previous_owner__username')
    ordering = ('-transaction_date',)
    
    fieldsets = (
        ('交易信息', {
            'fields': ('book', 'previous_owner', 'transaction_price', 'condition_at_sale', 'transaction_date')
        }),
    )
    
    list_per_page = 20
    date_hierarchy = 'transaction_date'
